<?php

/**
 * 
 */
class SettingSpecialService extends Service{

    public static function indexHeader(){
        $header = array(
            'id'            => array('text' => '序号', 'width' => '80px'),
            'name'          => array('text' => '专服名称', 'width' => '160px'),
            'special_id'    => array('text' => '专服ID', 'width' => '200px'),
        	'login_key'     => array('text' => 'LOGIN_KEY', 'width' => '200px'),
        	'charge_key'    => array('text' => 'CHARGE_KEY', 'width' => '200px'),
        	'status'        => array('text' => '状态', 'width' => '100px'),
        	'syn_info'        => array('text' => '同步情况', 'width' => '300px'),
        	'create_time'   => array('text' => '创建时间', 'width' => '150px'),
            'action'        => array('text' => '操作', 'width' => '60px'),
        );

        return $header;
    }

    public static function indexData($params){
        $modSettingSpecial = new ModSettingSpecial();
        $result = $modSettingSpecial->page($params);

        $isSynList = self::isSynList();
        $length = count($result['data']);
        foreach($result['data'] as $k=>&$v){
        	$type = 1;
        	if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
        		$type = 2;
        	}

            $v['create_time']     = date('Y-m-d H:i:s', $v['create_time']);
            $v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=settingSpecial/edit').'&id='.$v['id'].'">编辑</a>'; 
            if($v['status'] != 2){
            	$v['action']    .= '&nbsp;&nbsp;<a href="javascript:;" name="synSpecial_one" data-id="'.$v['id'].'">同步</a>';
            }
            //$v['action']        .= '&nbsp;&nbsp;<a href="'.PubFunc::buildBackUrl('/oss/?r=settingSpecial/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
            
            $id = $v['id'];
            if($v['status'] == 2){
            	$checkbox = '<input value="'.$v['id'].'" data-status="'.$show_status.'"  disabled="disabled" name="id" type="checkbox">&nbsp;&nbsp;';	
            }else{
            	$checkbox = '<input value="'.$v['id'].'" data-status="'.$show_status.'" name="id" type="checkbox">&nbsp;&nbsp;';
            }
            
            $v['id'] = $checkbox.$v['id'];
            
            $v['status'] = $isSynList[$v['status']];
        }
        unset($v);

        return $result;
    }

    public static function doAdd($params){
        $user = AccAuth::me();

        $modSettingSpecial = new ModSettingSpecial();
        $modSettingSpecial->game    = $params['g'];
        $modSettingSpecial->name    = $params['name'];
        $modSettingSpecial->special_id    = $params['special_id'];
        $modSettingSpecial->create_user   = $user['UserID'];
        $modSettingSpecial->create_time   = time();
        $modSettingSpecial->update_user   = $user['UserID'];
        $modSettingSpecial->update_time   = time();
        $modSettingSpecial->login_key = $params['login_key'];
        $modSettingSpecial->charge_key = $params['charge_key'];
        $modSettingSpecial->status = 1;
        return $modSettingSpecial->save();
    }

    public static function doEdit($id, $params){
        if(empty($id)) return array('state'=>false,'msg'=>'非法参数');

        $user = AccAuth::me();

        $modSettingSpecial = new ModSettingSpecial();
        $update = array();
        $update['game']         = $params['g'];
        $update['name']         = $params['name'];
        $update['special_id']   = $params['special_id'];
        $update['update_user']  = $user['UserID'];
        $update['update_time']  = time();
        $update['login_key']    = $params['login_key'];
        $update['charge_key']   = $params['charge_key'];
        $update['status'] = 7;
        $update['syn_info'] = '';
        $result = $modSettingSpecial->updateByPk($id, $update);
        if($result !== false){
        	$return_result = array('state'=>true,'msg'=>'编辑成功');
        }else{
        	$return_result = array('state'=>false,'msg'=>'编辑失败');
        }
        return $return_result;
        
        /**
        if($result !== false){
        	//同步
        	$d = array();
        	$d['special_id'] = $params['special_id'];
        	$d['login_key'] = $params['login_key'];
        	$d['charge_key'] = $params['charge_key'];
        	$send_info = self::send($d);
        	//修改数据的同步状态
        	$update = array();
        	$update['status'] = $send_info['api_status'];
        	$update['syn_info'] = $send_info['info_res'];
        	$modSettingSpecial->updateByPk($id, $update);
        	
        	$info_res .= '序号为 '.$id.' 的数据同步执行情况：<br/>'.$send_info['info_res'];
        	
        	$return_result = array('state'=>true,'msg'=>$info_res);
        }else{
        	$return_result = array('state'=>false,'msg'=>'');
        }
        return $return_result;
        **/

    }
    
    

    public static function get($id){
    	$modSettingSpecial = new ModSettingSpecial();
        $obj = $modSettingSpecial->findByPk($id);
        if($obj){
            $info = $obj->getAttributes();
            return $info;
        }else{
            return false;
        }
    }

    
    
    /*
     * 去重
     */
    public static function repeat($id = 0, $field, $val,$g){
    	$modSettingSpecial = new ModSettingSpecial();
    	$conut = $modSettingSpecial->repeat($id, $field, $val,$g);
    	return $conut;
    }
    
    /**
     * 获取专服列表
     * @return multitype:unknown
     */
    public static function specialList(){
    	$game = PubBase::getCurrentGame();
    	$params = array();
    	$params['g'] = $game;
    	$modSettingSpecial = new ModSettingSpecial();
    	$data = $modSettingSpecial->select($params);
    	$list = array();
    	foreach ($data as $key=>$val){
    		$list[$val['id']] = $val['name'].' ('.$val['special_id'].')';
    	}
    	return $list;
    		
    }
    
    public static function delete($id){
    	if(empty($id)) return false;
    
    	$modSettingSpecial = new ModSettingSpecial();
    	$modSettingSpecial->deleteByPk($id);
    	return true;
    }
    
    public static function isSynList(){
    	return array(
    			'1' => '未同步(添加时)',
    			'2' => '成功',
    			'3' => '失败',
    			'4' => '失败(编辑时)',
    			'5' => '部分失败',
    			'6' => '部分失败(编辑时)',
    			'7' => '未同步(编辑时)',
    	);
    }
    
    /**
     * 同步
     * @param unknown $ids
     */
    public static function synSpecial($ids){
    	if($ids == ''){
    		$out = array('status'=>0,'msg'=>'非法参数','data'=>'');
    		return $out;
    	}
    	
    	$modSettingSpecial = new ModSettingSpecial();
    	$list = $modSettingSpecial->setListForIds($ids);
    	$info_res = '';
    	$user = AccAuth::me();
    	foreach ($list as $key=>$val){
    		$d = array();
    		$d['special_id'] = $val['special_id'];
    		$d['login_key'] = $val['login_key'];
    		$d['charge_key'] = $val['charge_key'];
//     		$a = array();
//     		$a['login_key'] = $val['login_key'];
//     		$a['charge_key'] = $val['charge_key'];
//     		$d[$val['special_id']] = $a;
    		$send_info = self::send($d);
    		//修改数据的同步状态
    		$update = array();
    		$update['status'] = $send_info['api_status'];
    		$update['syn_info'] = $send_info['info_res'];
    		$update['update_user']  = $user['UserID'];
    		$update['update_time']  = time();
    		$modSettingSpecial->updateByPk($val['id'], $update);
    		
    		$info_res .= '序号为 '.$val['id'].' 的数据同步执行情况：<br/>'.$send_info['info_res'];
    	}

    	$out = array('status'=>1,'msg'=>$info_res,'data'=>'');
    	return $out;
    	
    }
    
    /**
     * 发送
     * 每一个服务器都需要发送，每调用一次send方法则发送一条数据
     */
    public static function send($params){
    	//每一个服务器都发送
    	//获取服务器列表
    	$modServer = new ModServer();
    	$modSdkServer = new ModSdkServer();
    	$game = PubBase::getCurrentGame();
    	$map['g'] = $game;
    	$serverList = $modServer->select($map);
    	$sdkserverList = $modSdkServer->select($map);
    	$info_res = '';
    	$len = count($serverList);
    	$success_len = $fail_len = 0;
    	foreach ($serverList as $key=>$val){
    		$api = new ApiService($val['id']);
    		$result = $api->setQuxuanSpecialServer($params);
    		if($result['state']){
    			$success_len++;
    			$info_res .= ' 发送服务器 <font color="red">'.$val['server_name'].' </font>成功<br/>';
    		}else{
    			$fail_len++;
    			$info_res .= ' 发送服务器 <font color="red">'.$val['server_name'].' </font>失败，失败原因：'.$result['msg'].'<br/>';
    		}
    	}
    	
    	$sdk_len = count($sdkserverList);
    	$len = $len + $sdk_len;
    	foreach ($sdkserverList as $key=>$val){
    		$api = new ApiSdkService($val['id']);
    		$result = $api->setQuxuanSpecialServer($params);
    		if($result['state']){
    			$success_len++;
    			$info_res .= ' 发送SDK服务器 <font color="red">'.$val['server_name'].' </font>成功<br/>';
    		}else{
    			$fail_len++;
    			$info_res .= ' 发送SDK服务器 <font color="red">'.$val['server_name'].' </font>失败，失败原因：'.$result['msg'].'<br/>';
    		}
    	}
    	
    	if($success_len == $len){ //全部成功
    		$api_status = 2;
    	}else if($fail_len == $len){ //全部失败
    		$api_status = 3;
    	}else{ //部分失败
    		$api_status = 5;
    	}
    	
    	return array('api_status'=>$api_status,'info_res'=>$info_res);

    }
    


}